<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\response;

class Resume extends Model {
	protected $fillable = ['user_id', 'owner_id', 'company_id', 'position_id', 'name', 'sex', 'mobile', 'area', 'job_date', 'birthday', 'position', 'edu', 'company_name', 'is_job'];
	protected function getDateFormat() {
		return time();
	}
	public static $rules = [
		'name' => 'required|max:12',
		'sex' => 'required',
		'mobile' => 'required|regex:/^[1][0-9]{10}$/',
		'area' => 'required|max:255',
		'birthday' => 'required|integer:10',
		'position' => 'required|max:255',
		'jobDate' => 'required|integer:10',
		'isJob' => 'required',
	];
	public static $message = [
		'name.required' => '姓名不能为空',
		'name.max' => '姓名最多12个字符',
		'sex.required' => '性别不能为空',
		'mobile.required' => '手机号必须填写',
		'mobile.regex' => '手机号格式不正确',
		'area.required' => '籍贯不能为空',
		'area.max' => '籍贯最多255个字符',
		'birthday.required' => '出生日期不能为空',
		'birthday.integer' => '出生日期格式不正确',
		'jobDate.required' => '参加工作时间不能为空',
		'jobDate.integer' => '参加工作时间格式不正确',
		'isJob.required' => '工作状态不能为空',
		'position.required' => '目标职位不能为空',
		'position.max' => '目标职位最多255个字符',
	];
	//上传图片
	public function upload_img(Request $request) {

		$file = $request->file('image');
		if (!$request->hasFile('image')) {
			return response()->json(['code' => 500, 'msg' => '上传文件为空或无效']);
		}
		if (!$request->file('image')->isValid()) {
			return response()->json(['code' => 500, 'msg' => $file->getErrorMessage()]);
		}
		if (!in_array($file->getClientOriginalExtension(), ['png', 'jpg', 'jpeg'])) {
			return response()->json(['code' => 500, 'msg' => '图片格式不允许']);
		}
		if ($file->getClientSize() > $file->getMaxFilesize()) {
			return response()->json(['code' => 500, 'msg' => '图片大小超过2M']);
		}
		$newFileName = md5(time() . rand(0, 10000)) . '.' . $file->getClientOriginalExtension();
		$savePath = 'image/' . date('Ym', time()) . '/' . $newFileName;
		$bytes = \Storage::put(
			$savePath,
			file_get_contents($file->getRealPath())
		);
		if (!\Storage::exists($savePath)) {
			return response()->json(['code' => 500, 'msg' => '上传文件出错,请重试']);
		}
		return $savePath;
	}
}
